package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsArea;
import com.econ.powercloud.ems.dal.mapper.EmsAreaMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zjf
 * @Description : emsArea DAO层
 * @Date : 2021/10/21 17:30
 * @Modified by : zjf
 */
@Repository
public class EmsAreaDAO extends ServiceImpl<EmsAreaMapper, EmsArea> {

    public Page<EmsArea> getProvinceList(String search, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<EmsArea> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(search), EmsArea::getCityName, search);
        lambdaQueryWrapper.isNotNull(EmsArea::getArea);
        Page<EmsArea> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    public List<EmsArea> getProvinceList() {
        return this.baseMapper.getProvinceList();
    }

    public List<EmsArea> getCityList() {
        LambdaQueryWrapper<EmsArea> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.isNull(EmsArea::getArea);
        lambdaQueryWrapper.isNotNull(EmsArea::getApiId);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    public Page<EmsArea> getCityPage(Integer province, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<EmsArea> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(province != null, EmsArea::getBelongto, province);
        lambdaQueryWrapper.isNull(EmsArea::getArea);
        lambdaQueryWrapper.isNotNull(EmsArea::getApiId);
        Page<EmsArea> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page, lambdaQueryWrapper);
    }

    public List<EmsArea> getListByCityCodeList(List<Integer> cityCodeList) {
        LambdaQueryWrapper<EmsArea> wrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.isNotEmpty(cityCodeList)){
            wrapper.in(EmsArea::getCityCode,cityCodeList);
        }else {
            wrapper.isNull(EmsArea::getCityCode);
        }
        return this.baseMapper.selectList(wrapper);
    }
}
